<?php
namespace App\Models\System;


use App\Models\Model;

class Tasks extends Model
{
    protected $table = 'tasks';

    protected $appends = [
        'type_text',
        'is_play'
    ];

    public function getTypeTextAttribute(){
        $type = !empty($this->attributes['type']) ? $this->attributes['type'] : '';
        if(!$type){
            return '';
        }
      
        if($type == 1){
            return '审核';
        }else if($type == 2){
            return '参与';
        }else if($type == 3){
            return '驳回';
        }
        return '';
    }

    //审核时间是否过期
    public function getIsPlayAttribute(){
        $start_date = !empty($this->attributes['start_date'])?strtotime($this->attributes['start_date']):'';
        $end_date = !empty($this->attributes['end_date'])?strtotime($this->attributes['end_date']):''; 
        $type = !empty($this->attributes['type']) ? $this->attributes['type'] : '';
        if($type == 1){
            if($start_date && $end_date){
                if($start_date == $end_date){
                    return 1;
                }
                $now_time = time();
           
                if($now_time <= $end_date){
                    return 1;
                }else{
                    return 0;
                }
            }     
            return 0;
        }else if($type == 2){
            $user_id = auth()->user()->id;
            $c_user_id = !empty($this->attributes['c_user_id']) ? $this->attributes['c_user_id'] : '';
            if($user_id != $c_user_id){
                return 0;
            }else{
                if($start_date && $end_date){
                    if($start_date == $end_date){
                        return 1;
                    }
                    $now_time = time();
               
                    if($now_time <= $end_date){
                        return 1;
                    }else{
                        return 0;
                    }
                }     
                return 0;
            }
        }else if($type == 3){
            $user_id = auth()->user()->id;
            $c_user_id = !empty($this->attributes['c_user_id']) ? $this->attributes['c_user_id'] : '';
            if($user_id != $c_user_id){
                return 0;
            }else{
                   
                return 1;
            }
        }
        return 0;
    }
    
}
